<?php
/*
	首页
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	if(!$_RQ['pay_id']){
		exi('请选择支付流水','error');
	}
	$this->session('pay_id',$_RQ['pay_id']);
	if($_RQ['pay_id']=='all'){
		$endprice=pdo_getsum('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1,'ispiao'=>0),'fee');
	}else{
		$paylog=pdo_get('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'id'=>$_RQ['pay_id']));
		if($paylog['status']!=1){
			exi('该支付流水，未支付！','error');
		}
		if($paylog['ispiao']){
			exi('该支付流水，已经开过发票！','error');
		}
		$endprice=$paylog['fee'];
	}
	$return['status']=$this->status;
	$return['type']=$this->type;
	$return['usertype']=$this->usertype;
	$return['sendtype']=$this->sendtype;
	$this->session('goback','jy_invoice/kai/');
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','realname','nickname'));
	$return['member']=$member;
	$file=array('address','id','mobile','name','pcd');
	$address=pdo_get('core_address',array('isdefault'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),$file);
	if(!$address){
		$address=pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),$file);
	}
	$address['pcd']=@json_decode($address['pcd'],true);
	$log=pdo_get('jy_invoice_list',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'','id desc');
	$to_name=$log['to_name']?:$member['realname'];
	$log['address']=@json_decode($log['address'],true);
	$return['post']=array(
		'type'=>0,
		'piao_type'=>$return['pluginset']['piao_type'],
		'to_name'=>$to_name,
		'to_email'=>$log['to_email'],
		'address'=>$log['address']?:$address,
		'endprice'=>$endprice,
	);
	$return['nowtypes']=$this->type;
	$return['pluginset']['piao_types']=$return['pluginset']['piao_types']?:'在线购物|软件服务|业务服务';
	$return['piao_types']=str_array($return['pluginset']['piao_types'],'|');
	if($return['pluginset']['type']==1){
		unset($return['nowtypes'][0]);
		unset($return['nowtypes'][2]);
		$return['post']['type']=1;
	}elseif(!$return['pluginset']['type']){
		unset($return['nowtypes'][1]);
		unset($return['nowtypes'][2]);
	}
	$return['list']=array();
	exi($return);
}
// 开票
if($_RQ['op']=='post'){
	$_RQ['pay_id']=$this->session('pay_id');
	if(!$_RQ['pay_id']){
		exi('请选择支付流水','error');
	}
	if($_RQ['pay_id']=='all'){
		$endprice=pdo_getsum('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1,'ispiao'=>0),'fee');
	}else{
		$paylog=pdo_get('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'id'=>$_RQ['pay_id']));
		if($paylog['status']!=1){
			exi('该支付流水，未支付！','error');
		}
		if($paylog['ispiao']){
			exi('该支付流水，已经开过发票！','error');
		}
		$endprice=$paylog['fee'];
	}
	$post=get_postdata();
	if(!$post['to_name']){
		exi('请填写抬头','error');
	}
	if($post['usertype']==2 && (!$post['to_code'] || !$post['to_bank'] || !$post['to_bankcode'])){
		exi('请填写税号,开户行,银行账号!','error');
	}
	if(!$post['address']['name'] || !$post['address']['mobile']){
		exi('请填写收票人名称,电话','error');
	}
	if(!$post['to_email']){
		exi('请填写收票人邮箱','error');
	}
	if(!$post['piao_type']){
		exi('请填写开票内容','error');
	}
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','realname','nickname'));
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'type'=>$post['type'],
		'sendtype'=>!$post['type']?0:1,
		'address'=>ijson_encode($post['address']),
		'usertype'=>$post['usertype'],
		'to_name'=>$post['to_name'],
		'to_code'=>$post['to_code'],
		'to_bank'=>$post['to_bank'],
		'to_bankcode'=>$post['to_bankcode'],
		'to_email'=>$post['to_email'],
		'piao_type'=>$post['piao_type'],
		'logsn'=>'SN'.get_ordersn($this->tablename('list'),'logsn'),
		'piao_company'=>$post['piao_company'],
		'piao_username'=>$member['realname']?:$member['nickname'],
		'piao_fee'=>$endprice,
		'pluginname'=>'core',
		'pay_id'=>$_RQ['pay_id'],
		'createtime'=>time(),
	);
	pdo_insert('jy_invoice_list',$data);
	if($_RQ['pay_id']=='all'){
		pdo_update('core_paylog',array('ispiao'=>1,'piaotime'=>time()),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'ispiao'=>0));
	}else{
		pdo_update('core_paylog',array('ispiao'=>1,'piaotime'=>time()),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'id'=>$_RQ['pay_id']));
	}
	exi('提交成功！','','/member.paylog/');
}
?>